package spring.cloud.service.task.analyzer;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import spring.cloud.service.task.domain.zooKeeperAndNamespace.ZooKeeper;
import spring.cloud.service.task.dto.StatisticsDto;
import spring.cloud.service.task.mapper.StatisticsDtoMapper;
import spring.cloud.service.task.service.TaskService;
import spring.cloud.service.task.service.ZooKeeperService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

@Slf4j
public class DashboardServiceImpl implements DashboardService {


    @Autowired
    private StatisticsDtoMapper statisticsDtoMapper;

    @Autowired
    private ZooKeeperService zooKeeperService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private StatisticsRefreshService statisticsRefreshService;

    private ExecutorService updateStatisticsThreadPool;


    @PostConstruct
    public void init() {
        updateStatisticsThreadPool = Executors.newSingleThreadExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                String name = "single-update-statistics";
                Thread t = new Thread(r, name);
                t.setDaemon(false);
                t.setPriority(Thread.NORM_PRIORITY);
                return t;
            }
        });
    }

    @PreDestroy
    public void destroy() {
        if (updateStatisticsThreadPool != null) {
            updateStatisticsThreadPool.shutdownNow();
        }
    }


    @Override
    public StatisticsDto top10LoadExecutor(String zklist) throws RuntimeException {
        return statisticsDtoMapper
                .findStatisticsByNameAndAddressList(StatisticsTableKeyConstant.TOP_10_LOAD_EXECUTOR, zklist);
    }

    @Override
    public String top10LoadExecutorByAllZkCluster() throws RuntimeException {
        List<ExecutorStatistics> executorStatisticsList = new ArrayList<>();
        Collection<ZooKeeper> collection = zooKeeperService.getOnlineZooKeeperList();
        for (ZooKeeper zooKeeperCluster : collection) {
            StatisticsDto statisticsDto = top10LoadExecutor(zooKeeperCluster.getAddressList());
            if (statisticsDto != null) {
                String result = statisticsDto.getStatistics();
                List<ExecutorStatistics> executorStatisticsList1 = JSON.parseArray(result, ExecutorStatistics.class);
                if (executorStatisticsList1 != null) {
                    executorStatisticsList.addAll(executorStatisticsList1);
                }
            }
        }
        executorStatisticsList = DashboardServiceHelper.sortExecutorByLoadLevel(executorStatisticsList);
        List<ExecutorStatistics> top10LoadExecutor = executorStatisticsList.subList(0, executorStatisticsList.size() > 9 ? 10 : executorStatisticsList.size());
        return JSON.toJSONString(top10LoadExecutor);
    }

    @Override
    public StatisticsDto top10LoadJob(String zooKeeperAddressList) throws RuntimeException {
        return statisticsDtoMapper.findStatisticsByNameAndAddressList(StatisticsTableKeyConstant.TOP_10_LOAD_JOB, zooKeeperAddressList);
    }

    @Override
    public String top10LoadJobByAllZkCluster() throws RuntimeException {
        List<TaskStatistics> taskStatisticsList = new ArrayList<>();
        Collection<ZooKeeper> collection = zooKeeperService.getOnlineZooKeeperList();
        for (ZooKeeper zooKeeperCluster : collection) {
            StatisticsDto statisticsDto = top10LoadJob(zooKeeperCluster.getAddressList());
            if (statisticsDto != null) {
                String result = statisticsDto.getStatistics();
                List<TaskStatistics> tempList = JSON.parseArray(result, TaskStatistics.class);
                if (tempList != null) {
                    taskStatisticsList.addAll(tempList);
                }
            }
        }
        taskStatisticsList = DashboardServiceHelper.sortJobByLoadLevel(taskStatisticsList);
        List<TaskStatistics> top10LoadJob = taskStatisticsList.subList(0, taskStatisticsList.size() > 9 ? 10 : taskStatisticsList.size());
        return JSON.toJSONString(top10LoadJob);
    }


}
